package com.k52group.vn.micromarket;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.k52group.vn.micromarket.R;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.classes.Service;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;
import com.k52group.vn.micromarket.utils.Utils;

public class SearchActivity extends Activity {
	Spinner sp;
	EditText et;
	Profile profile;
	Button btsearch, btfav, btmessage, btprofile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		sp = (Spinner) findViewById(R.id.spinner1);
		et = (EditText) findViewById(R.id.EditText02);
		btsearch = (Button) findViewById(R.id.Button01);
		btfav = (Button) findViewById(R.id.Button03);
		btmessage = (Button) findViewById(R.id.Button02);
		btprofile = (Button) findViewById(R.id.Button04);

		String data_user = Utils.getData(Constant.USER, this);
		profile = new Gson().fromJson(data_user, Profile.class);
		if (profile.getUserId() == 0) {
			new LoadServiceTask(this, sp, true).execute();
			btprofile.setVisibility(View.GONE);
		} else
			new LoadServiceTask(this, sp, false).execute();

		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	public void onClick(View v) {
		if (v.getId() == R.id.Button01)
			new SearchTask().execute();
		else if (v.getId() == R.id.Button02) {
			Intent it = new Intent(this, InboxActivity.class);
			startActivity(it);
		} else if (v.getId() == R.id.Button03) {
			Intent it = new Intent(this, FavActivity.class);
			startActivity(it);
		} else if (v.getId() == R.id.Button04) {
			Intent it = new Intent(this, RegisterActivity.class);
			String profile = Utils.getData(Constant.USER, this);
			it.putExtra("data", profile);
			startActivity(it);
		}
	}

	private class SearchTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		boolean isError = false;
		String respone;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(SearchActivity.this);
			dialog.setMessage("Searching ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				HttpRequest request = HttpRequest.post(Constant.URL_SEARCH);
				Service service = (Service) sp.getSelectedItem();
				request.form("userid", profile.getUserId() + "");
				request.form("service", service.getId() + "");
				request.form("about", et.getText());

				request.form("regId", Utils.getData(Constant.GCMID, SearchActivity.this));
				request.form("dns", Utils.getDeviceID(SearchActivity.this));
				Location location = Utils.getLocation(SearchActivity.this);
				if (location != null) {
					request.form("lat", location.getLatitude());
					request.form("long", location.getLongitude());
				}

				respone = request.body();
			} catch (Exception e) {
				e.printStackTrace();
				isError = true;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			if (isError) {
				Toast.makeText(SearchActivity.this, "No Data !", Toast.LENGTH_SHORT).show();
			} else {
				forward(respone);
			}
		}
	}

	private void forward(String data) {
		Intent it = new Intent(SearchActivity.this, HitListActivity.class);
		it.putExtra("data", data);
		System.out.println("data " + data);
		startActivity(it);
	}
}
